﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;
using System.Data;
using System.Threading;
using System.Globalization;

public partial class companypart_CompanyFollowup : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
             
            if (Request.QueryString["check_id"] != null)
            {
                main.Visible = false;
                DataRow ds = DB.getdata("select d.*,t.* ,d.id as mysub ,t.id as pro_id from daily_checking d   INNER JOIN company_product t ON (d.company_product_id = t.id)   WHERE  d.id=" + Request.QueryString["check_id"]).Tables[0].Rows[0];
                actualdate.Text = ds["issuedate"].ToString();
                DatePicker1.SelectedDateString = DateTime.Parse(ds["issuedate"].ToString()).ToShortDateString();
                ViewState["maindata"] = ds["mysub"].ToString() + "," + ds["distributer_id"].ToString() + "," + ds["publisher_id"].ToString();
                ViewState["product"] = ds["pro_id"].ToString();
                supposedno.Text = ds["copyno"].ToString();
               // SqlDataSource3.SelectCommand = " select i.id, i.name,i.internal,c.assumedcopyno from internal_distribution i inner join internal_copies c on (i.id=c.internal_id) where  c.company_product_id=" + ViewState["product"].ToString();
              //  SqlDataSource3.SelectParameters["id"].DefaultValue = ViewState["product"].ToString();
              //  internals.DataBind();
                follows.Visible = true;
            }
            //"select p.*,t.* ,t.id as mysub,f.* from frequencies f  INNER JOIN products p  ON (f.id = p.freq_id) INNER JOIN company_product t ON (t.product_id = p.id)   WHERE t.company_id = @company_id and t.[enddate]>getdate()"
        }
    }
    protected override void InitializeCulture()
    {
        if (Session["CurrentCulture"] != null)
        {
            string lang = Session["CurrentCulture"].ToString();
            if (lang != null && lang != "")
            {
                Thread.CurrentThread.CurrentUICulture = new CultureInfo(lang);
                Thread.CurrentThread.CurrentCulture = CultureInfo.CreateSpecificCulture(lang);
                Thread.CurrentThread.CurrentCulture.DateTimeFormat = new CultureInfo("en-US", false).DateTimeFormat;
                // Thread.CurrentThread.CurrentCulture.Calendar=new  System.Globalization.GregorianCalendar();
               
            }
        }
    }
    protected void senddis_Click(object sender, EventArgs e)
    {
        string dd = ViewState["maindata"].ToString().Split(',')[1];
        if (dd != "0")
        {
            DataSet ds = DB.getdata("select email1 from distributers where id=" + dd);
            Sendmail1.sets(ds.Tables[0].Rows[0][0].ToString(),RoleEnum.Organization,RoleEnum.Admin,this.Session["company_id"].ToString(),"");
            Page.ClientScript.RegisterStartupScript(this.GetType(), "repor", "there();", true);
        }
    }
    protected void sendpup_Click(object sender, EventArgs e)
    {
          string dd= ViewState["maindata"].ToString().Split(',')[2];
          if (dd != "0")
          {
              DataSet ds = DB.getdata("select email1 from publishers where id=" + dd);
              Sendmail1.sets(ds.Tables[0].Rows[0][0].ToString(),RoleEnum.Organization,RoleEnum.Organization,this.Session["company_id"].ToString(),"");
              Page.ClientScript.RegisterStartupScript(this.GetType(), "repor", "there();", true);

          }
    }
    protected void sendadmin_Click(object sender, EventArgs e)
    {
        Sendmail1.sets("daily_checking_id:int:"+ ViewState["maindata"].ToString().Split(',')[0] ,RoleEnum.Organization,RoleEnum.Admin,this.Session["company_id"].ToString(),"" );
        Page.ClientScript.RegisterStartupScript(this.GetType(), "repor", "there();", true);


    }
    protected void aore_Click(object sender, EventArgs e)
    {
        LinkButton lk = (LinkButton)sender;
       string mydate  = ((Label)lk.NamingContainer.FindControl("issuedate")).Text;
        supposedno.Text = ((Label)lk.NamingContainer.FindControl("copyno")).Text;
       actualdate.Text = mydate;
       DatePicker1.SelectedDateString = DateTime.Parse(mydate).ToShortDateString();
        string bb = lk.ToolTip;
        ViewState["maindata"] = bb;
        delv.Focus();
        follows.Visible = true;
    }
    protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (RadioButtonList1.SelectedValue == "0")
        {
            sends.Visible = true;
            ready.Value = "0";
           // received.Visible = false;

        }
        else
        {
            ints();
            //bool x = RequiredFieldValidator1.IsValid;
                
        }
    }

    public void ints()
    {
        SqlDataSource3.SelectCommand = " select i.id, i.name,i.internal,c.assumedcopyno from internal_distribution i inner join internal_copies c on (i.id=c.internal_id) where  c.company_product_id=" + ViewState["product"].ToString();
         internals.DataBind();
         ready.Value = "1";
        sends.Visible = false;
         ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "hj", "there2();", true);
           
    
    }
    protected void currentsubs_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (currentsubs.SelectedValue != "0")
        {
            ViewState["product"] = currentsubs.SelectedValue;
           
        }
    }
    protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {

        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    e.Row.Attributes.Add("onmouseover", "this.Mystyle=this.style.backgroundColor;this.style.backgroundColor='#FFFFA8'");

        //    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=this.Mystyle");
        //}
    }
    
   
    protected void save_Click(object sender, EventArgs e)
    {
        if (ready.Value == "0")
        {
            List<string> fields = new List<string>();
            ArrayList values = new ArrayList();
            List<string> types = new List<string>();
            ///////////////
            /////////////////////////////
            fields.Add("datechecked");
            types.Add("datetime");
            values.Add(DateTime.Now.ToString("yyyy-MM-dd"));
            /////////////////////////////
            string ll = DatePicker1.SelectedDate.Year.ToString();
            if (ll != "1")
            {
                fields.Add("receiveddate");
                types.Add("datetime");
                values.Add(DatePicker1.SelectedDate.ToString("yyyy-MM-dd"));
                /////////////////////////////
            }
            fields.Add("delivered");
            types.Add("int");
            values.Add(RadioButtonList1.SelectedValue);
            ///////////////////////
           string f= Session["user"].ToString();
           fields.Add("user_id");
           types.Add("int");
           values.Add(Session["user"].ToString().Split(',')[1]);
           ///////////////////////
            fields.Add("issueno");
            types.Add("string");
            values.Add(fix.Text);
            ///////////////////////
            fields.Add("checked");
            types.Add("int");
            values.Add(1);
            ///////////////////////
            ///////////////////////
            if (RadioButtonList1.SelectedValue != "0")
            {
                fields.Add("receivedcopy");
                types.Add("int");
                values.Add(delv.Text);
            }
            ///////////////////////
            //////////////////
            GridView1.DataBind();
            /////////////////////////////////////////////
            int ff = (int)DB.UPDATE("daily_checking", "int,id," + ViewState["maindata"].ToString().Split(',')[0], fields, types, values);
            /////////////////////////////////////////////////////////////////////
            if (RadioButtonList1.SelectedValue == "0")
            {
                foreach (GridViewRow gvr in internals.Rows)
                {
                    TextBox vv = (TextBox)gvr.FindControl("copyno");
                    vv.Text = "0";
                }
            }
                foreach (GridViewRow gvr in internals.Rows)
                {

                    fields.Clear();
                    types.Clear();
                    values.Clear();
                    //////////////////
                    TextBox vv = (TextBox)gvr.FindControl("copyno");
                    fields.Add("daily_checking_id");
                    types.Add("int");
                    values.Add(ViewState["maindata"].ToString().Split(',')[0]);
                    ///////////////////////

                    fields.Add("internal_id");
                    types.Add("int");
                    values.Add(vv.ToolTip.Split(',')[0]);
                    ///////////////////////
                    fields.Add("assumedcopyno");
                    types.Add("int");
                    values.Add(vv.ToolTip.Split(',')[1]);
                    ///////////////////////
                    fields.Add("actualcopyno");
                    types.Add("int");
                    values.Add(vv.Text);
                    ///////////////////////
                    fields.Add("receiveddate");
                    types.Add("datetime");
                    values.Add(DatePicker1.SelectedDate.ToString("yyyy-MM-dd"));
                    /////////////////////////////

                    /////////////////////////////
                    fields.Add("datechecked");
                    types.Add("datetime");
                    values.Add(DateTime.Now.ToString("yyyy-MM-dd"));
                    /////////////////////////////
                    ///////////////////////

                    fields.Add("company_product_id");
                    types.Add("int");
                    values.Add(ViewState["product"].ToString());
                    ///////////////////////
                    ff = (int)DB.INSERT("internal_checking", fields, types, values);
                }
           
            Response.Redirect("~/companypart/CompanyNotifications.aspx");
        }
    }
    protected void history_Click(object sender, EventArgs e)
    {
        //  PostBackUrl='<%# this.ResolveUrl("~/companypart/" + string.Format("HistoryOfCheckings.aspx?company_product={0}",currentsubs.SelectedValue)) %>' 
        Response.Redirect("~/followerpart/HistoryOfCheckings.aspx?company_product=" + ViewState["product"].ToString());
    }
    protected void delv_TextChanged(object sender, EventArgs e)
    {
        if (RadioButtonList1.SelectedValue == "1")
        {
            ints();
        }
    }
}
